<?php

	require_once("CheckLoginForm.php");	
	require_once("src/view/LoginForm.php"); 
	require_once("src/model/ValidateLoginInfo.php"); 
	require_once("src/view/LoggedInView.php");	
	require_once("src/view/PageView.php");	


class MasterController
{	/**
 	* $string. The html we'll echo out.	 
 	*/
	private $html = "";
		
		
	public function CheckWhatHTMLToShow() // TODO: lång jäkel
	{
		$pageView = new PageView();
		$loggedInPage = new LoggedInView();
		$checkLoginFormAndGetSessions = new CheckLoginForm();
		$checkLoginFormAndGetSessions->CheckIfSubmitted();
			
		$sessionMessage = $checkLoginFormAndGetSessions->GetMessageSession();
		$sessionLastEnteredUsername = $checkLoginFormAndGetSessions->
									  GetLastEnteredUsernameSession();
		$sessionLogin = $checkLoginFormAndGetSessions->GetLoginSession();
		$this->DefaultIPandUserAgentValues();
	
		if($this->GetUserAgentSession() != $_SERVER["HTTP_USER_AGENT"] ||
		   $this->GetIPSession() != $_SERVER["REMOTE_ADDR"])
		{
			$this->GetLoginForm("Tjuvjävel!",""); 
			$this->DestroyLoginCookies();
			$checkLoginFormAndGetSessions->SetMessageSession("");
			$checkLoginFormAndGetSessions->SetLastEnteredUsernameSession(""); 
			$checkLoginFormAndGetSessions->SetLoginSession(false);
			return $this->html;
		}		
		else 
		{
				
			// IF FINNS OK KAKA SOM ÄR OK, och stämmer överens.
			if($this->CookiesExistsAndIsOK() && $sessionLogin === false &&
			   !array_key_exists('logout', $_GET))
			{	
				// KALLA PÅ LOGGA IN SIDAN.
				$this->GetLoginPage("", "VI KOM IHÅG DIG!");
				$checkLoginFormAndGetSessions->SetLoginSession(true);
				$checkLoginFormAndGetSessions->SetMessageSession(""); 
				return $this->html;
			}
			else 
			{   
				if($sessionLogin === true && !array_key_exists('logout', $_GET) || 
				   !$this->CookiesExistsAndIsOK()) 
				{	// om sessionskakor != "" och !CookiesExist
					if( isset($_COOKIE['username']) && isset($_COOKIE['userpassword']) && 
					    !$this->CookiesExistsAndIsOK())
					{
						$this->GetLoginForm("Fiffla inte med kakorna!",""); 
						$checkLoginFormAndGetSessions->SetLoginSession(false);
						$checkLoginFormAndGetSessions->SetLastEnteredUsernameSession("");
						$this->DestroyLoginCookies();
					}
					else 
					{
						$this->GetLoginPage($sessionLastEnteredUsername, $sessionMessage);										    	
						$checkLoginFormAndGetSessions->SetMessageSession(""); // 
					}	
			 	}

				if(array_key_exists('logout', $_GET))   
				{	
					if($sessionLogin === true)
					{					
						$this->GetLoginForm("Du har lyckats logga ut!",""); 
						$this->DestroyLoginCookies();
					}
					else // om någon skriver logout i urlen. när vi inte är inloggade.
					{
						$this->GetLoginForm("",""); 
					}			
					$checkLoginFormAndGetSessions->SetLoginSession(false);
					$checkLoginFormAndGetSessions->SetLastEnteredUsernameSession("");
				}
				if ($sessionLogin === false && !isset($_COOKIE['username']) && 
				    !isset($_COOKIE['userpassword']))
				{
					$this->GetLoginForm($sessionMessage, $sessionLastEnteredUsername); 
				}
				return $this->html;
			}			
		}
	}


	public function GetLoginForm($formMessage, $lastEnteredUsername)
	{	
		$loginForm = new LoginForm(); 
		$pageView = new PageView();
		
		$theLoginForm = $loginForm->GetForm($formMessage,$lastEnteredUsername);									
		$this->html = $pageView->HtmlPage("Laboration2PHP", "", $theLoginForm);
	}

	public function GetLoginPage($LastEntUsername, $message)
	{
		$loggedInPage = new LoggedInView();
		$pageView = new PageView();
		
		$loggedInHTML = $loggedInPage->LoggedInPage($LastEntUsername, $message);	
		$this->html = $pageView->HtmlPage("Laboration2PHP1", "", $loggedInHTML);
	}
	
		
		
	public function CookiesExistsAndIsOK()
	{
		//TODO: Check if username and password cookies exists, and that they haven't
		// been fooked around with. 
		$validateLoginInfo = new ValidateLoginInfo();
		if(isset($_COOKIE['username'])&& $_COOKIE['userpassword'])
		{
			if($validateLoginInfo->CookieLoginValidation( 
						$_COOKIE["username"],$_COOKIE["userpassword"]))
			{
				return true;
			}	
			else 
			{
				return false;
			}
			
		}
		else 
		{
			
			return false;		
		}		
	}	
	
	public function DestroyLoginCookies()
	{
		setcookie("username", false);
		setcookie("userpassword",false);
	}

	
	
	
	
	public function DefaultIPandUserAgentValues()
	{
		if(isset($_SESSION['Webbläsare']) == false)
		{
			  $_SESSION['Webbläsare'] = $_SERVER["HTTP_USER_AGENT"];
		}
		 
		if(isset($_SESSION['ip']) == false)
		{
			  $_SESSION['ip'] = $_SERVER["REMOTE_ADDR"];
		}		
	}
	
	/**
	 * @Return string, ip adrs till användaren.
	 */
	public function GetIPSession()
	{
		 return $_SESSION['ip'];
		 
	}
	 
	 
	 /**
	 * @Return string, vilken webbläsare.
	 */
	public function GetUserAgentSession()
	{
		
		 return $_SESSION['Webbläsare'];
		
	}
	
	/**
	 * @Return string, username
	 */
	public function GetCookieUsername()
	{
		 
		if(isset($_COOKIE['username']))
		{
			return $_COOKIE['username'];
		}
		else
		{
			return "";
		} 
	}
	 
	 
	 /**
	 * @Return string, userpassword
	 */
	public function GetCookieUserPassword()
	{
		
		if(isset($_COOKIE['userpassword']))
		{
			 return $_COOKIE['userpassword'];
		}
		else
		{
			return "";
		} 
	}

	
}
	
	
	
	
	
	
	
	